﻿$(".refresh").on("click", function() {
    location.reload();
});

(function(window) {

var config = {
	language : {
		"zh-cn" : "Lang/zh-cn.js",
		"en" : "Lang/en.js"
	}
};

var lang, d = new Date(), userAgent = navigator.userAgent.toLowerCase();
window.isIphone = userAgent.indexOf("iphone") >= 0;
( !isIphone && !window.WebApi ) && $(".header,.footer").css("display", "block");

d.setFullYear(d.getFullYear() + 1);

// 检测语言，先从url中取
if ( /lang=([a-z\-]+)/.test( location.href.toLowerCase() ) ) {
    lang = RegExp.$1;
}
// 从cookie中取
if (!lang) {
    lang = $.cookie.get("lang");
}
// 从userAgent中取
if (!lang) {
    lang = ( userAgent.split(";")[3] || "" ).trim();
}

if ( !/^zh-/.test(lang) ) {
    lang = "en";
}

$.cookie.set("lang", lang, {
    expires : d,
    path : "/"
});


// 检测时区
var tz = $.cookie.get("tz");
if (!tz) {
    /[+-]\d+$/.test(d);
    tz = RegExp.lastMatch;
    $.cookie.set("tz", tz, {
        expires : d,
        path : "/"
    });
}

$.ajax.getJSON({
	url: config.language[lang] || config.language["zh-cn"],
	async: false,
	useRndNum : false,
	onSuccess: function(Lang) {
	    window.Lang = Lang;
	
		Lang && $("[data-lang]").each(function(){
		    var lang = Lang[ this.getAttribute("data-lang") ];
		    if (lang != null) {
		        if ("INPUT" === this.nodeName || "TEXTAREA" === this.nodeName) {
		            this.value = lang;
		        } else {
			        this.innerHTML = lang;
			    }
			}
		});
	}
});


var doc = document.documentElement,
    getScrollTop = window.getScrollTop = function() {
        return doc.scrollTop || document.body.scrollTop || window.pageYOffset;
    };


var loading = $("#loading");
loading.show = function() {
    this.each(function(el) {
        el.style.width = Math.max(doc.scrollWidth, doc.clientWidth) + "px";
        el.style.height = Math.max(doc.scrollHeight, doc.clientHeight) + "px";
        
        var child = el.firstChild;
        child.style.width = doc.clientWidth + "px";
        child.style.height = doc.clientHeight + "px";
        
        child.style.marginTop = getScrollTop() + "px";
        
        el.style.display = "block";
    });
};
loading.hide = function() {
    this.each(function(el) {
        el.style.display = "none";
    });
};
window.loading = loading;

$("body").css("font-size", Lang["font-size"]);


var div = document.createElement("div");
div.setAttribute("ontouchstart", "return;");
window.isTouch = "function" === typeof div.ontouchstart;
div = null;


var cacheNames = {
    LEAGUE : "FocusedLeagues",
    FOCUSMATCHES : "FocusedMatches",
    MATCH : "FocusedMatches",
    NICKNAME : "Nickname",
    EVENT : "Event"
};

window.getCacheName = function(name, ball) {
    return ( ball ? ball.toUpperCase() + "_" : "" ) + cacheNames[ name.toUpperCase() ];
};


})(window);


// 选足球或篮球
function MatchTypeSelect(val){
	var href = location.href.toLowerCase();
	switch(val){
		case 0:
			if( -1 == href.indexOf("football") ){
				location.href = href.replace("/basketball/", "/football/");
			}
		break;
		case 1:
			if( -1 == href.indexOf("basketball") ){
				location.href = href.replace("/football/", "/basketball/");
			}
		break;
	} 
}

// 客户端初始化参数
function GetPanelParam(which, isObj){
	var ret = {
		ChatPnl: { },
		TopPnl: {		/* 控件传值时显示 */
			TopTitle: { },
			TopLefSpn: {
				OnSelected: "MatchTypeSelect",
				Options: [
					{
						Text: Lang["football"],
						Value: "0"
					},{
						Text: Lang["basketball"],
						Value: "1"
					}
				]
			}
		}
	}, chatPnl = ret.ChatPnl, topPnl = ret.TopPnl;
	
	switch(which){
		case "match":
			topPnl.TopTitle = {
				Text : Lang["matches-title"]
			};
		break;
		
		case "odds":
			topPnl.TopTitle = {
				Text : Lang["odds-title"]
			};
		break;
		
		case "rank":
			topPnl.TopTitle = {
				Text : Lang["rank-title"]
			};
		break;
		
		case "chat":
			topPnl.TopTitle = {
				Text : Lang["chat-title"]
			};
		break;
		
		case "league":
			topPnl.TopTitle = {
				Text : Lang["league-title"]
			};
			delete topPnl.TopLefSpn;
		break;
		
		case "live":
			topPnl.TopLefBon = { Text: Lang["back"] };
			delete topPnl.TopLefSpn;
			ret.MenuPnl = { Visibility: "false" };
		break;
	}
	
	return isObj ? ret : JSON.stringify(ret);
}


window.localCache = window.WebApi ? function(name, val) {
    if (arguments.length >= 2) {
        WebApi.SetCache(name, val == null ? "" : val);
    } else {
        return String( WebApi.GetCache(name) );
    }
} : function(name, val) {
    if (arguments.length >= 2) {
        if (null == val) {
            delete localStorage[name];
        } else {
            localStorage[name] = val;
        }
    } else {
        return localStorage[name] || "";
    }
};


window.showInfo = window.WebApi ? function(msg, hideDelay) {
    window.WebApi.ShowInfo(msg, hideDelay || "0");
} : function(msg, hideDelay) {
    console.log(msg + "," + hideDelay);
};